package com.weakteam.orderonline.adminconsole.gwt.client.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.weakteam.orderonline.adminconsole.gwt.client.domain.Category;

/**
 * Created by ZabbarovMZ on 22.04.2014.
 */
public class MenuGoodsWidget extends Composite {

    interface MenuGoodsWidgetUiBinder extends UiBinder<Widget, MenuGoodsWidget> { }
    private static MenuGoodsWidgetUiBinder uiBinder = GWT.create(MenuGoodsWidgetUiBinder.class);

    @UiField  protected MenuGoodsControl goodsControl;
    @UiField  protected Label caption;
    @UiField  protected AbsolutePanel dragSpace;
    public MenuGoodsContainer goodsContainer;

    @UiFactory
    MenuGoodsControl makeMenuContentControl() { // method name is insignificant
        return new MenuGoodsControl();
    }


    public MenuGoodsWidget() {
        initWidget(uiBinder.createAndBindUi(this));
        this.goodsContainer = new MenuGoodsContainer(dragSpace);
        this.goodsControl.addButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                goodsContainer.addNewItem();
            }
        });
        this.goodsControl.editButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                goodsContainer.editSelectedItem();
            }
        });
        this.goodsControl.removeButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                goodsContainer.deleteSelectedItem();
            }
        });
        this.goodsContainer.addSelectionHandler(new SelectionHandler<MenuGoodsBox>() {
            @Override
            public void onSelection(SelectionEvent<MenuGoodsBox> event) {
                goodsControl.setItemSelectedMode(true);
            }
        });
    }

    public void prepare(Category category) {
        this.goodsContainer.clear();
        this.goodsContainer.setCategory(category.getId());
        this.caption.setText(category.getName());
        this.goodsControl.setItemSelectedMode(false);
    }

}
